<template>
	<view class="content">
		<view class="sticky-box">
			<view class="flex-row">
				<text class="tip-text">无此商品码对应信息，请在选择对应的商品</text>
			</view>
			<search-input ref="searchInput"
				@searchConfirm="handleSearch($event)"></search-input>
		</view>
		<view class="goods-list">
			<view class="goods-item"
				:class="{'goods-active': index === activeIndex}"
				@tap="handleActiveGoods(item, index)"
				v-for="(item, index) in goodsList" :key="index">
				<view class="flex-row goods-header">
					<view class="goods-name">
						<text>{{item.Name}}</text>
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.Manufacturer}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.Spec}}/{{item.Unit}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.ApprovalNumber}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.Manufacturer_Supplier}}
					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 154rpx;" v-if="footerBtn"></view>
		<view class="flex-row footer-btn" v-if="footerBtn">
			<view class="common-solid-btn" @tap="cancel()">
				取消
			</view>
			<view class="common-btn" @tap="handleSaveCheck()">
				保存
			</view>
		</view>
		<!-- 包装比例弹窗 -->
		<uni-popup ref="packPopup" type="center" :is-mask-click="false">
			<view class="flex-col pack-popup">
				<view class="success-tip">
					<text>包装比例</text>
				</view>
				<view class="flex-row popup-input-title">
					<text>包装比例</text>
				</view>
				<view class="flex-row popup-input">
					<input v-model="packRate" type="number" placeholder="请输入包装比例">
				</view>
				<!-- <view class="flex-row popup-input-title">
					<text>包装单位</text>
				</view>
				<view class="flex-row popup-input">
					<input v-model="packUnit" type="text" placeholder="请输入包装单位">
				</view> -->
				<view class="flex-row popup-input-title">
					<text style="flex: 1;">是否采码</text>
					<view class="">
						<switch :checked="isScanCode" @change="(e) => isScanCode = e.detail.value" color="#1983e8"
							style="position: relative;top: -14rpx;transform:scale(0.7);right: -24rpx;"/>
					</view>
				</view>
				<view class="flex-row popup-btn">
					<view class="popup-left-btn" @tap="closePopup('packPopup')">
						取消
					</view>
					<view class="popup-right-btn" @tap="handleSave()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 对码成功弹窗 -->
		<uni-popup ref="successPopup2" type="center" :is-mask-click="false">
			<view class="flex-col success-popup2">
				<view class="success-tip">
					<text>对码成功</text>
				</view>
				<view class="success-content2">
					<text>您已成功将条码与商品信息绑定</text>
				</view>
				<!-- <view class="success-content2">
					<text>点击采集溯源码或</text>
				</view>
				<view class="success-content2">
					<text>标记无溯源码</text>
				</view> -->
				<view class="flex-row popup-btn2">
					<view class="popup-center-btn" @tap="back">
						确定完成
					</view>
				</view>
				<!-- <view class="success-content2">
					<text>此商品还有其他条码对应</text>
				</view>
				<view class="flex-row popup-btn2">
					<view class="popup-center-btn" @tap="keepContrast">
						继续对码
					</view>
				</view> -->
				<!-- <view class="flex-row popup-btn">
					<view class="popup-left-btn" @tap="back">
						标记无溯源码
					</view>
					<view class="popup-right-btn" @tap="back">
						采集溯源码
					</view>
				</view> -->
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	var that;
	export default {
		data() {
			return {
				searchVal: '',
				defaultManufacturer: '',
				footerBtn: true,
				goodsList: [],
				activeIndex: -1,
				packRate: 1,   // 包装比例
				packUnit: '',   // 包装单位
				isScanCode: false,
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				scanGoodsCode: state => state.goods.scanGoodsCode,  // scanGoodCode
			})  
		},
		onLoad() {
			that = this
			uni.setNavigationBarTitle({
				title: `采码：${that.scanGoodsCode}`
			})
		},
		onShow() {
			that.getServerData()
		},
		methods: {
			getServerData() {
				that.getGoodsList()
			},
			getGoodsList() {
				let filter = that.searchVal + '@' + that.defaultManufacturer;
				let requestData = {
					MethodCode: "App_Deal_GoodsCheckCode",
					Token: that.Token,
					Info: {
						"UserCode": that.userInfo.UserCode,
						"Kind": 'New_CheckCode_GetHisGoods',
						"Filter": filter,   // 条码
					}
				};
				//console.log(requestData)
				that.$request.globalRequest('', 'POST', requestData, '', 0).then(res => {
					console.log(res.data);
					let data = res.data.Data
					if (data.length > 0) {
						that.goodsList.concat(data)
					} 
				}).catch( res => {
					console.log(res)
				})
			},
			handleSearch(res) {
				that.searchVal = res.value
				that.getGoodsList()
			},
			handleActiveGoods(item, index) {
				that.activeIndex = index;
			},
			cancel() {
				that.back()
			},
			back() {
				that.$Router.back()
			},
			openPopup(popup) {
				this.$refs[popup].open()
			},
			closePopup(popup) {
				this.$refs[popup].close()
			},
			handleSaveCheck() {
				if (that.activeIndex === -1) {
					that.$common.showNormalToast('请选择对码商品');
					return
				}
				that.packRate = 1
				that.saveHisGood()
				// that.openPopup('packPopup')
			},
			handleSave() {
				that.saveHisGood()
			},
			saveHisGood() {
				if (that.packRate === '') {
					that.$common.showNormalToast('请输入包装比例');
					return
				}
				// if (that.packUnit.trim() === '') {
				// 	that.$common.showNormalToast('请输入包装单位');
				// 	return
				// }
				const goodId = that.goodsList[that.activeIndex].Code
				const IsSupervisionCode = that.isScanCode ? '1' : '0';
				// ${that.packUnit.trim()}@
				let filter = `${that.scanGoodsCode }@${goodId}@${that.packRate}@${IsSupervisionCode}` 
				let requestData = {
					MethodCode: "App_Deal_GoodsCheckCode",
					Token: that.Token,
					Info: {
						"UserCode": that.userInfo.UserCode,
						"Kind": 'New_CheckCode_SaveHisGoods',
						"Filter": filter,   // 条形码@商品编码@比例@单位
					}
				};
				console.log(requestData)
				that.$request.globalRequest('', 'POST', requestData, '', 0).then(res => {
					console.log(res.data);
					let data = res.data.Data
					// that.closePopup('packPopup')
					that.openPopup('successPopup2')
				}).catch( res => {
					that.$common.showNormalToast(res);
					console.log(res)
				})
			},
			openPopup(popup) {
				this.$refs[popup].open()
			},
			closePopup(popup) {
				this.$refs[popup].close()
			},
		}
	}
</script>

<style lang="scss" scoped>
@import "goodsList.scss";
</style>
